package com.example.PriceCompare;

import java.io.IOException;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.location.Address;
import android.location.Geocoder;
import android.os.Bundle;
import android.util.Log;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.google.android.maps.GeoPoint;


public class PreMain extends Activity {
	
	private Button drawRouteButton,getCoordinatesButton;
	private EditText fromEditText,fromCoordinates;
	private EditText toEditText,toCoordinates;
	private Address address;
	private int flag1=0,flag2=0;
	private double tempFromLati, tempFromLongi;
	private String fromString, tostring;
	private GeoPoint fromPoint,toPoint;
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
			Log.i("tag", "act started");
			super.onCreate(savedInstanceState);
            setContentView(R.layout.test);
            Bundle bundle = this.getIntent().getExtras();
            Log.i("tag", "errorcode1");
            tempFromLati=bundle.getDouble("fromlat");
            Log.i("tag", "errorcode11");
            tempFromLongi=bundle.getDouble("fromlong");
            Log.i("tag", "errorcode111");
            tostring=bundle.getString("toAdd");
//          tostring=intent.getCharSequenceExtra("toAdd").toString();
//            tostring = new String("Big G, Madgaon");
            Log.i("tag", "errorcode2");
            mapThisLocation();
	}
	
	protected void mapThisLocation(){
//		fromString=fromEditText.getText().toString();
//        toString=toEditText.getText().toString();
		Geocoder g = new Geocoder(this);
		List<Address> addresses;

		try {
			do{
			addresses=g.getFromLocationName(tostring, 1);
//			Toast.makeText(this,"not found",Toast.LENGTH_SHORT).show();
			Log.i("tag", "errorcode3");
			}while(addresses.size()<=0);
			if(addresses.size()>0){
				
				address=addresses.get(0);
				address.getLatitude();
				address.getLongitude();
//				toPoint= new GeoPoint((int) (address.getLatitude() * 1E6),(int) (address.getLongitude() * 1E6));
				String add="";
				
				for(int i=0;i<address.getMaxAddressLineIndex();i++){
					add+=address.getAddressLine(i) + "\n";
				}
//				Toast.makeText(this,add,Toast.LENGTH_LONG).show();
//				Toast.makeText(this,"("+address.getLongitude()+","+address.getLatitude()+")",Toast.LENGTH_SHORT).show();
				flag2=1;
			}
			else{
//				Toast.makeText(this, "We Failed to Locate Destination", Toast.LENGTH_SHORT).show();
				flag2=0;
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
//			Toast.makeText(this, "No idea!!!", Toast.LENGTH_SHORT).show();
			flag2=0;
		}
		
//		double tempFromLati=(double)fromPoint.getLatitudeE6()/1E6;
//		double tempFromLongi=(double)fromPoint.getLongitudeE6()/1E6;
		Log.i("tag", "errorcode4"+flag2);
//		double tempToLati=(double)toPoint.getLatitudeE6()/1E6;
//		double tempToLongi=(double)toPoint.getLongitudeE6()/1E6;
//		Toast.makeText(this,"This one is using a double",Toast.LENGTH_SHORT).show();
//		Toast.makeText(this,"("+tempFromLati+","+tempFromLongi+")",Toast.LENGTH_SHORT).show();
		if(flag2==1){
		Intent intentForMain= new Intent().setClass(getApplicationContext(), RouteTestMain.class);
		intentForMain.putExtra("fromLongi", tempFromLongi);
		intentForMain.putExtra("fromLati", tempFromLati);
		intentForMain.putExtra("toLongi", address.getLongitude());
		intentForMain.putExtra("toLati", address.getLatitude());
		Log.i("tag", "errorcode5");
//		Toast.makeText(this,"("+tempFromLati+","+tempFromLongi+")",Toast.LENGTH_SHORT).show();
//		Toast.makeText(this,"("+address.getLatitude()+","+address.getLongitude()+")",Toast.LENGTH_SHORT).show();
				startActivity(intentForMain);
		}
		else{
//			Toast.makeText(getApplicationContext(), "One of the Locations couldn't be found", Toast.LENGTH_SHORT);
		}
	}
	
}